<?php

/**
 * This is the model class for table "image".
 *
 * The followings are the available columns in table 'image':
 * @property integer $id
 * @property integer $group_id
 * @property string $name
 * @property integer $priority
 * @property string $alt
 */
class Image extends AActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Image the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'image';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('group_id, name, priority, alt', 'required'),
			array('priority', 'numerical', 'integerOnly'=>true),
			array('group_id', 'length', 'max'=>10),
			array('name', 'length', 'max'=>100),
			array('alt', 'length', 'max'=>250),
			array(
				'name',
				'unique',
				'className'=>'Image',
				'attributeName'=>'name',
				'criteria'=>array(
					'condition'=>'t.group_id=:group_id',
					'params'=> array(
						':group_id' => $this->group_id
					)
				)
			),
			array('id, group_id, name, priority, alt', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'group'=>array(self::BELONGS_TO,'ImageGroup','group_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'group_id' => 'Group',
			'name' => 'Name',
			'priority' => 'Priority',
			'alt' => 'Alt',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('group_id',$this->group_id,true);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('priority',$this->priority);
		$criteria->compare('alt',$this->alt,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	public function getDeleteConfig(){
		return array();
	}

	/**
	 * @param Image $item
	 */
	public function doDelete($item){
		$image = Yii::app()->basePath . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR .
			AImageUpload::getImagesFolder() . DIRECTORY_SEPARATOR . $item->group->folder->folder_name .
			DIRECTORY_SEPARATOR . $item->group_id . DIRECTORY_SEPARATOR . $item->name;
		parent::doDelete($item);
		if(file_exists($image)){
			unlink($image);
		}
	}
}